Skip to content

Conversation

joshuahannan
Copy link
Member

@joshuahannan joshuahannan commented Jul 9, 2025

Implements the contracts and tests for the Scheduled Callbacks FLIP

Copy link
Collaborator

@janezpodhostnik janezpodhostnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work so far. I had some comments. Not sure if they were already addressed in the other PRS

/// calculate fee by converting execution effort to a fee in Flow tokens.
access(all) fun calculateFee(executionEffort: UInt64, priority: Priority, data: AnyStruct?): UFix64 {
// Use the official FlowFees calculation
let baseFee = FlowFees.computeFees(inclusionEffort: 1.0, executionEffort: UFix64(executionEffort))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This links to my comment here onflow/flips#331 (comment).

You are adding inclusion fees here, so you do not need to have a minimum execution effort limit. I would have the inclusion effort configurable though.

Copy link
Member Author

@joshuahannan joshuahannan Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes sense to have a configurable inclusion fee because that would require us to predict network load for the future, which isn't possible, right? We'd have to predict it for each timestamp. Additionally, the fee multipliers are already configurable, so that kind of solves the same problem as a configurable inclusion fee. I also responded to your comment on the FLIP about the minimum effort

originalTimestamp: sanitizedTimestamp,
priority: priority,
executionEffort: executionEffort,
fees: <- fees,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fees include the storageFee but the since its still in the CallbackData it doesn't actually help the account holding this data with storage capacity. Is this ok? do we just plan to have a high enough balance on the account for this to not be a problem?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is a good point. I think that we'll have enough FLOW in the account that we won't have to worry about it, but we still should probably deposit those extra storage fees to the account's flow vault.

Now that I am thinking about it, I kind of want to just deposit all fees to the account's vault temporarily instead of storing them in CallbackData. It simplifies the contract but it does create a bigger honeypot that would be worse if a vulnerability was found. How does that sound?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a big concern, probably we ran out of computation before storage.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bluesign what do you mean? like the computation of storing a large piece of data is more costly than the actual storage fees?

Copy link
Contributor

@bluesign bluesign Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I think adding ( writing ) some bytes ( considering it is small ) to a large dictionary ( where we may have hypothetical problem ) will be more cheaper in storage fees vs computation required.

It is pretty hard to predict as we don't have fees technically, and luckily no-one is abusing.


// if there is no space left for medium priority we search for next available timestamp
// todo: check how big the callstack can grow and if we should avoid recursion
return self.calculateScheduledTimestamp(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we try doing this without recursion? Recursion is costly.

Copy link
Member Author

@joshuahannan joshuahannan Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How costly is recursion? I don't see this ever getting to more than a few levels deep and this is a clean way to do it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think stack depth is 2000, cost to authorizer is negligible ( though to system it can have heavier cost )

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With recursion the memory cost is a lot higher which doesn't really translate to fees, but could halt the transaction. the execution cost is also higher, but not by that much, I cant really give you any concrete numbers at this time.

If you feel its cleaner this way we can keep it if the stack isn't going to be too deep most of the time.

* Optimise processing with sorted timestamps

* Add tests for sorted timestamps type

* Fix wrong assert args

* Extract garbage collection

* Add missing arg

* Handle wrong statuses

* Change access control to account

* Change access control to only limit process and execute to FVM

* Add comments

* Regenerate all the assets

* generate assets

---------

Co-authored-by: Joshua Hannan <[email protected]>
@joshuahannan
Copy link
Member Author

Thanks for the feedback @janezpodhostnik! I opened a PR addressing some of your comments and left questions and comments on the other ones

let type = data!.getType()
if type.isSubtype(of: Type<Number>())
|| type.isSubtype(of: Type<Bool>())
|| type.isSubtype(of: Type<Path>())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The size of Path is not any more bounded than e.g. a String so seems counterintuitive to round it to 0.0 regardless of length.

See e.g.:

transaction {
    execute {
        var stringToStore = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        log("Length of string: ".concat(stringToStore.length.toString()))
        log(
            "getSizeOfData if stored as string:".concat(
                FlowCallbackScheduler.getSizeOfData(
                    stringToStore
                ).toString()
            )
        )
        log(
            "getSizeOfData if masked as path:".concat(
                FlowCallbackScheduler.getSizeOfData(
                    StoragePath(identifier: stringToStore)
                ).toString()
            )
        )
    }
}

Produces:

6:51AM INF LOG: "Length of string: 510"     
6:51AM INF LOG: "getSizeOfData if stored as string:0.00057500"
6:51AM INF LOG: "getSizeOfData if masked as path:0.00000000"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. I didn't consider that! I'll remove Path from the list

if callbackEffort <= lowPriorityEffortAvailable {
lowPriorityEffortAvailable = lowPriorityEffortAvailable - callbackEffort
lowPriorityCallbacks.remove(key: lowCallbackID)
sortedCallbackIDs.append(lowCallbackID)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FLIP says that a low priority callback "is executed opportunistically, only in blocks after the scheduled timestamp".

However, it seems this code may process low-priority callbacks before they are due - lowPriorityCallbacks is a collection of all low-priority callbacks as they are all stored at the special timeslot 0.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh man, I can't believe I didn't have a test for that. Good catch!

Priority.Medium: mediumPriorityEffortReserve + sharedEffortLimit,
Priority.Low: 5_000
},
minimumExecutionEffort: 5,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: FLIP says "The provided execution effort value must be bigger than 10". Even though this is configurable, would IMO make sense to align the default value and the value mentioned in the FLIP.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. We'll update that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I'll update that

joshuahannan and others added 3 commits August 25, 2025 12:35
* Remove shared state access

* do cleanup in process

* refactor finalize and only call after execution

* fix initial tests

* fix low priority bug and initialize canceled callbacks to 0

* give low priority callbacks their own slots

* make test

* Isolate pending queue creation

* Add test for pending queue

* Typo

* fix joshs comments from unfinished PR

* update comments

* reschedule low priority if they don't fit any more

* add used and pending queue tests

* fix test files

* fix get status and cancel status

* rename files and add cancel tests

---------

Co-authored-by: gregor <[email protected]>
* add getName, description, and more event parameters

* add length restrictions to name and description

* add status check for ID 0
* add getName, description, and more event parameters

* add length restrictions to name and description

* add status check for ID 0

* keep FLOW in account and add callback getters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants